package com.baizhi.controller;

import com.baizhi.entity.Admin;
import com.baizhi.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 野马 on 2019/6/14.
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private HttpServletRequest request;

    @RequestMapping("/login")
    public String login(String username,String password){
        HttpSession session = request.getSession();

        System.out.println(username+password+"..............");

        Admin admin = adminService.login(username, password);

        /**
         * 如果不为空则放到session作用域中
         */
        if (admin != null){
            session.setAttribute("admin",admin);
            System.out.println("登陆");
            return "redirect:/main/main.jsp";
        }else {
            return "redirect:/login.jsp";
        }

    }
    //退出登陆
    @RequestMapping("/quit")
    public String quit(){
        System.out.println(request.getSession().getAttribute("admin")+"ahahhaha");
        request.getSession().removeAttribute("admin");
        return "redirect:/login.jsp";

    }

    //修改密码
    @RequestMapping("updatePassword")
    @ResponseBody
    public Map updatePassword(String oldPassword,String newPassword){
        System.out.println(oldPassword+newPassword+"123456789");
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        String password = admin.getPassword();
        int id = admin.getId();
        Map map = new HashMap();

        if(password.equals(oldPassword)){

            try {
                adminService.updatePassword(newPassword,id);
                map.put("pd",true);
                request.getSession().removeAttribute("admin");
            } catch (Exception e) {
                map.put("pd",false);
                e.printStackTrace();
            }
        }else {
            map.put("no",false);
        }
        return map;
    }

    @RequestMapping("insert")
    @ResponseBody
    public Map insert(Admin admin){
        Map map = new HashMap();
        try {
            adminService.insertAdmin(admin);
            map.put("insert",true);
        } catch (Exception e) {
            map.put("insert",false);
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Map delete(int id){
        Map map = new HashMap();
        try {
            adminService.deleteAdmin(id);
            map.put("insert",true);
        } catch (Exception e) {
            map.put("insert",false);
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("update")
    @ResponseBody
    public Map update(Admin admin){
        Map map = new HashMap();
        try {
            adminService.updateAdmin(admin);
            map.put("insert",true);
        } catch (Exception e) {
            map.put("insert",false);
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("selectList")
    @ResponseBody
    public Map selectAdmin(int page,int rows){
        return adminService.selectList(page, rows);
    }






}
